package com.tx.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.tx.admin.annotation.RbacMapping;
import com.tx.admin.controller.po.EntityPo;
import com.tx.admin.controller.po.PagePo;
import com.tx.admin.controller.vo.EntityVo;
import com.tx.admin.controller.vo.PageVo;
import com.tx.admin.entity.SuperEntity;
import com.tx.admin.service.SuperService;

import io.swagger.annotations.ApiOperation;

public abstract class SuperController<E extends SuperEntity> {

	@Autowired
	private SuperService<E> superService;
	
	@ApiOperation("基础添加或修改")
	@PostMapping("/save")
	@RbacMapping("修改")
	public EntityVo<E> save(@RequestBody EntityPo<E> po) {
		E entity = po.getEntity();
		this.superService.save(entity);
		EntityVo<E> vo =new EntityVo<>();
		vo.setEntity(entity);
		vo.setMsg("修改数据成功");
		return vo;
	}
	  
	
	@ApiOperation("基础删除")
	@PostMapping("/delete")
	@RbacMapping("删除")
	public EntityVo<E> delete(@RequestBody EntityPo<E> po) {
		E entity = po.getEntity();
		this.superService.delete(entity);
		EntityVo<E> vo =new EntityVo<>();
		vo.setEntity(entity);
		vo.setMsg("删除数据成功");
		return vo;
	}
	
	
	@ApiOperation("基础查询")
	@PostMapping("/page")
	@RbacMapping("查询")
	public PageVo<E> page(@RequestBody PagePo<E> params) {
		Page<E> page = this.superService.findAll(params);
		PageVo<E> vo =new PageVo<>();
		vo.setPage(page);
		vo.setMsg("查询数据成功");
		return vo;
	}
	
}
